package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.dao.newsRepostity;
import com.example.demo.model.WeichuListResponse;
import com.example.demo.model.newsTable;
import com.example.demo.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by zouwenchang on 2018/1/26.
 */
@RestController
public class newsController {

    @Autowired
    newsRepostity newsRepostity;



    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @GetMapping(value = "/newsList")
    private String projectList() {
        WeichuListResponse weichuRsp=new WeichuListResponse();

        weichuRsp.setMsg("success");
        weichuRsp.setCode("0000");
        weichuRsp.setItems((ArrayList) newsRepostity.findAll());

        return JSON.toJSONString(weichuRsp);
    }

    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping(value = "/addNew")
    private newsTable addHuman(@RequestParam("file") MultipartFile file,
                             @RequestParam("Id")  Integer Id ,
                            HttpServletRequest request, HttpServletResponse response) throws IOException {
        newsTable pros=new newsTable();

        MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request);
        String fileName = file.getOriginalFilename();


        try {
            FileUtil.saveFile( fileName,file.getBytes());

        } catch (Exception e) {
        }




        String urls= FileUtil.getURL(fileName);

        pros.setNewsPic(urls);

        pros.setId(Id);
        pros.setNewsTitle(params.getParameter("newsTitle"));
        pros.setNewsDesc(params.getParameter("newsDesc"));

        //返回json

        return newsRepostity.save(pros);
    }
    /**
     * 删除一个人员
     *
     * @param id
     */
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping(value = "/deleteNews/{id}")
    public void humanDelete(@PathVariable("id") Integer id) {

        newsRepostity.delete(id);
    }

    /**
     * 更新一个人事
     *

     */
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping(value = "/editNews/{id}")
    private newsTable editHuman(@RequestParam("file") MultipartFile file,
                                    @PathVariable("id") Integer Id,
                                    HttpServletRequest request, HttpServletResponse response) throws IOException {
        newsTable prod = new newsTable();

        MultipartHttpServletRequest params=((MultipartHttpServletRequest) request);
        String fileName = file.getOriginalFilename();


        try {
            FileUtil.saveFile( fileName,file.getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        }

        String urls= FileUtil.getURL(fileName);

        prod.setNewsPic(urls);

        prod.setId(Id);
        prod.setNewsTitle(params.getParameter("newsTitle"));
        prod.setNewsDesc(params.getParameter("newsDesc"));

        //返回json

        return newsRepostity.save(prod);
    }
}
